<?php
/**
 * 状态为3待确认，paytime超过指定，自动确认
 */

if (!defined('APP_PATH')) {
    define('APP_PATH', __DIR__ . '/application/');
// 加载框架引导文件
    require __DIR__ . '/thinkphp/start.php';
}


function confirm_order(){
    echo "\r\n开始时间：".date('Y-m-d H:i:s')."\r\n";

    $redis = rds();

    $rkey = 'confirm_order';
    if ($redis->get($rkey)) {
        $this->error('正在运行');
    }else{
        $redis->set($rkey,1);
        $redis->expire($rkey,3600);
    }

    $Otcorder = new  \app\admin\model\Otcorder;

    //状态为3待确认，paytime超过指定，自动确认
    $min = config('site.otc_confirm_time');

    $pretime = strtotime("-".$min." min");

    $orders = db('otc_order')->where("state=3")->where("paytime<={$pretime}")->select();

    if (empty($orders)) {
        $redis->del($rkey);
        $redis->close();

        exitthis('没有订单');
    }

    foreach ($orders as $key => $val) {
        $redis_key = 'otc_order_'.$val['id'];
        if ($redis->get($redis_key)) {
            unset($orders[$key]);
        }else{
            $redis->set($redis_key,1);
            $redis->expire($redis_key,300);
        }
    }

    foreach ($orders as $order){
        $Otcorder->dealOrder($order, 4);
        $redis_key = 'otc_order_' . $order['id'];
        $redis->del($redis_key);
    }

    $redis->del($rkey);

    $redis->close();

    exitthis('执行完成');

}

function exitthis($msg = '')
{
    echo '执行结束时间：'.date('Y-m-d H:i:s')."\r\n";

    exit($msg);
}

confirm_order();